package com.seeyon.apps.ebs.controller;

import com.alibaba.fastjson.JSONObject;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.authenticate.domain.User;
import com.seeyon.ctp.common.controller.BaseController;
import com.seeyon.ctp.common.log.CtpLogFactory;
import org.apache.commons.logging.Log;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class LoginToArchiveController extends BaseController {

    private static final Log log = CtpLogFactory.getLog(LoginToArchiveController.class);

    public void getArchiveUrl(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
         //获取登录名
        User user = AppContext.getCurrentUser();
        String loginName = user.getLoginName();
        if (loginName.matches("^[\u4e00-\u9fa5].*")) {
            loginName = URLEncoder.encode(loginName, "UTF-8");
        } else {
            // 如果以英文开头,则把第一个字母变成小写
            loginName = URLEncoder.encode(Character.toLowerCase(loginName.charAt(0)) + loginName.substring(1), "UTF-8");
        }
        String loginUrl = "http://172.16.2.38/isp/service/seeloginauth/auth/userloginintegration?account=" + loginName;
        JSONObject returnResult = new JSONObject();
        returnResult.put("code",200);
        returnResult.put("data",loginUrl);
        response.setCharacterEncoding("UTF-8");
        try {
            response.getWriter().write(returnResult.toString());
        } catch (IOException e) {
            log.error("写出单点url失败", e);
        }
    }

    public ModelAndView loginToArchive(HttpServletRequest request, HttpServletResponse response){
        return new ModelAndView("ebs/loginToArchive");
    }

}
